package com.sesxh.project.knowledge.biz.service.impl;

import java.util.Map;
import java.util.Optional;
import com.sesxh.frame.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.sesxh.frame.core.util.ZBeanUtil;
import com.sesxh.frame.core.util.ZDateUtil;
import com.sesxh.frame.core.util.ZAlert;
import com.sesxh.frame.core.util.ZAssert;
import com.sesxh.frame.dao.ParamMap;
import com.sesxh.project.knowledge.biz.service.IFavoriteService;
import com.sesxh.project.knowledge.biz.dao.FavoriteMapper;
import com.sesxh.project.knowledge.biz.model.Favorite;
import java.util.stream.Stream;
@Service
public class FavoriteServiceImpl implements IFavoriteService{
 	@Autowired
	private FavoriteMapper favoriteMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createFavorite(Favorite favorite) {
		ZBeanUtil.setCreateInfo(favorite);
		ZBeanUtil.validateBean(favorite);
		favoriteMapper.insertSelective(favorite);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Favorite> loadFavoriteById(Long id) {
		if(id!=null){
			return Optional.ofNullable(favoriteMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateFavorite(Favorite favorite) {
		ZBeanUtil.setUpdatedInfo(favorite);
		ZBeanUtil.validateBean(favorite);
		favoriteMapper.updateByPrimaryKeySelective(favorite);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteFavorite(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		favoriteMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setFavoriteStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		favoriteMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadFavoriteDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(favoriteMapper.selectListCount(pm), favoriteMapper.selectMapListPage(pm));
		return ds;
	}
	
}